﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MixScript.Appl.Lib;

namespace MixScriptWeb.Share
{
    public partial class searchhelp : System.Web.UI.Page
    {
        private  static cSearchHelp osch;

        private string[] zField;

        public string[] ZField
        {
            get { return zField; }
            set { zField = value; }
        }

        private string zReturnDesc;

        public string ZReturnDesc
        {
            get { return zReturnDesc; }
            set { zReturnDesc = value; }
        }


        private string zSearchParam;

        public string ZSearchParam
        {
            get { return zSearchParam; }
            set { zSearchParam = value; }
        }


        private static DataTable dt;
        private static SortDirection sd;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {              
                hdControl.Value = Request.QueryString["scontrol"];
                hdForm.Value = Request.QueryString["sform"];
                hdVal.Value = Request.QueryString["sval"];
                hdlbl.Value = Request.QueryString["slbl"];
                hdsearchid.Value = Request.QueryString["searchid"];
                osch = new cSearchHelp(hdsearchid.Value);
                txtSearch.Text = hdVal.Value;
                populateGrid();
             
            }
       

        }

        void populateGrid()
        {
            try
            {
                 dt=new DataTable();
            //    osch = new cSearchHelp();
            //    if (osch.FillGrid(ref dt, "sy_user", new string[] { "userid", "userpassword" }))
            //    {
            //        GridView1.DataSource = dt;
            //        GridView1.DataBind();
            //    }

                if (hdsearchid.Value.Trim() == string.Empty)
                {
                    lblmsg.Text="SearchID cannot be empty";
                }

                else
                {

                    if (hdVal.Value.Trim() == string.Empty)
                    {

                        if (osch.FillGrid(ref dt, osch.sTableName, osch.arrField))
                        {
                            bindtogv(dt);
                        }

                    }
                    else
                    {
                        btnSearch_Click(this, new EventArgs());
                    }
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                 dt = new DataTable();
                osch.SearchText(ref dt, osch.sTableName, osch.arrField, txtSearch.Text);
                bindtogv(dt);

            }
            catch (Exception ex)
            {

                lblmsg.Text = ex.Message;
            }
        }

        public string ShortDirection(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }

            return newSortDirection;
        }


        public void setSortExpression(GridView gv, string[] arrSortExp)
        {
            try
            {
                setSortExpression(gv, arrSortExp, 1);
            }
            catch (Exception ex)
            {

                //   throw ex;
            }

        }
        public void setSortExpression(GridView gv, string[] arrSortExp, int idxStart)
        {
            try
            {
                int i = 0;
                foreach (string s in arrSortExp)
                {
                    if (s != string.Empty)
                    {
                        gv.Columns[i + idxStart].SortExpression = s;
                    }
                    i++;
                }
            }
            catch (Exception ex)
            {

                //  throw;
            }

        }

        public void bindGridview(GridView gv, DataView dv)
        {
            try
            {
                gv.DataSource = dv;
                gv.DataBind();


            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public void bindGridview(GridView gv, DataView dv, LinkButton lb)
        {
            try
            {
                gv.DataSource = dv;
                gv.DataBind();
                lb.Visible = dv.Count > 0;

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public void bindGridview(GridView gv, DataTable dt, LinkButton lb)
        {
            try
            {
                gv.DataSource = dt;
                gv.DataBind();
                lb.Visible = dt.Rows.Count > 0;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void bindGridview(GridView gv, DataTable dt)
        {
            try
            {
                gv.DataSource = dt;
                gv.DataBind();
                //lb.Visible = dt.Rows.Count > 0;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        void bindtogv(DataTable dt)
        {
            try
            {

                gvSearchHelp.DataSource = dt;
                gvSearchHelp.DataBind();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int idx = 0;
                if (int.TryParse((string)e.CommandArgument, out  idx) && e.CommandName!="Page")
                {
                    GridViewRow rw = ((GridView)sender).Rows[idx];
                    // (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                    string sScrpt1 = string.Format("send2Child('{0}', '{1}', '{2}');", hdForm.Value, hdControl.Value, rw.Cells[1].Text);
                    //string sScrpt = string.Format("send2Child('{0}', '{1}', '{2}');", hfForm.Value, hfCntrl1.Value, LONG_DSC);
                    string sScrpt = "javascript:window.opener.document.forms['" + hdForm.Value + "'].elements['" + hdControl.Value + "'].value = '" + rw.Cells[1].Text + "';";
                    // sScrpt += "window.opener.document.forms['" + hdForm.Value + "'].elements['" + hdControl.Value + "'].value = '" +"ssss" + "'";
                    if (rw.Cells.Count > 2)
                    {
                        sScrpt += "javascript:window.opener.document.getElementById('" + hdlbl.Value + "').innerHTML= '" + rw.Cells[2].Text + "';";
                    }
                    sScrpt += "javscript:window.close();";

                    ClientScriptManager cs = this.ClientScript;
                    if (!cs.IsClientScriptBlockRegistered(this.GetType(), "kk"))
                    {
                        cs.RegisterClientScriptBlock(this.GetType(), "kk", sScrpt, true);
                    }                    
                }                
            }
            catch (Exception ex)
            {

                lblmsg.Text = ex.Message;
            }
        }

        protected void gvSearchHelp_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSearchHelp.PageIndex = e.NewPageIndex;
            //gvdetail.DataBind();
            bindGridview(gvSearchHelp, dt);
        }

        protected void gvSearchHelp_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (sd == SortDirection.Ascending)
            {
                sd = SortDirection.Descending;
            }
            else
            {
                sd = SortDirection.Ascending;
            }

            DataView dataView = new DataView(dt);
            dataView.Sort = e.SortExpression + " " + ShortDirection(sd);

            bindGridview(gvSearchHelp, dataView);
        }

      


    }
}